function [fb_arr,pb_arr,vis_arr,run_arr,ffm_arr]=fig3a(proj_meta,sites)

fb_arr=[];
pb_arr=[];
vis_arr=[];
run_arr=[];
ffm_arr=[];
sites=[4:5];
ini_cnt=1;
l_win=10;
r_win=5;
data_lwin=5;
data_rwin=35;
for knd=1:length(sites)
    cur_site=sites(knd)
    all_act={};
    pl_cells=[];
    for tnd=1:size(proj_meta(cur_site).rd,2)
        cnt=0;
        for vnd=1:size(proj_meta(cur_site).rd,1)
            all_act{tnd}(cnt+1:cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1),:)=proj_meta(cur_site).rd(vnd,tnd).act;
            cnt=cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1);
            pl_cells(vnd)=cnt;
        end
    end
    fb_snip={};
    pb_snip={};
    fb_av={};
    pb_av={};
    for tnd=1:size(proj_meta(cur_site).rd,2)
        nbr_frames=proj_meta(cur_site).rd(1,tnd).nbr_frames;
        stim_id=proj_meta(cur_site).rd(1,tnd).ps_id;
        runP=proj_meta(cur_site).rd(1,tnd).velP_smoothed>0.005;
        runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
        runM_raw=proj_meta(cur_site).rd(1,tnd).velM_smoothed;
        for lnd=1:size(all_act{1},1)
            act=all_act{tnd}(lnd,:);
            [fb_av{lnd,tnd},pb_av{lnd,tnd},fb_snip{lnd,tnd},pb_snip{lnd,tnd}]=pert_ana_meta(nbr_frames,stim_id,runM,runP,runM_raw,act);
        end
    end
    for tnd=1:size(proj_meta(cur_site).rd,2)
        cnt=ini_cnt;
        for lnd=1:size(all_act{1},1)
            if ~isempty(fb_snip{lnd,tnd}{13})
                fb_arr(cnt,:,tnd)=mean(fb_snip{lnd,tnd}{13},2)';
            else
                fb_arr(cnt,:,tnd)=zeros(1,41);
            end
            if ~isempty(pb_snip{lnd,tnd}{13})
                pb_arr(cnt,:,tnd)=mean(pb_snip{lnd,tnd}{13},2)';
            else
                pb_arr(cnt,:,tnd)=zeros(1,41);
            end
            cnt=cnt+1;
        end
    end    
    blink_mat={};
    blink_dark_mat={};
    [~,res_mat{1},res_mat{2},~,~,~,~,~,~,~,~,other{knd}]=meta_get_cell_type_onset(proj_meta,cur_site);
    num_tp=size(res_mat{1},2);
    num_cell=size(res_mat{1},1);
    for tnd=1:size(proj_meta(cur_site).rd,2)
        cnt=ini_cnt;
        for lnd=1:size(all_act{1},1)
            if ~isempty(res_mat{1}{lnd,tnd})
                cur_vis=res_mat{1}{lnd,tnd};
                vis_arr(cnt,:,tnd)=mean(cur_vis,1);
            end
            cnt=cnt+1;
        end
    end
    for tnd=1:size(proj_meta(cur_site).rd,2)
        runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
        ons=find(diff(runM)==1);
        ons(find(diff(ons)<data_rwin+data_lwin)+1)=[];
        run_snips=[];
        cnt=1;
        for gnd=1:length(ons)
            if ~(ons(gnd)-data_lwin<1)&&~(ons(gnd)+data_rwin+10>length(runM))
                if sum(runM(ons(gnd)-l_win:ons(gnd)))==0 & sum(runM(ons(gnd)+1:ons(gnd)+r_win))==r_win
                    run_snips(cnt,1)=ons(gnd)-data_lwin;
                    run_snips(cnt,2)=ons(gnd)+data_rwin;
                    cnt=cnt+1;
                end
            end
        end
        cnt=ini_cnt;
        for lnd=1:size(all_act{1},1)
            cur_act=all_act{tnd}(lnd,:);
            cur_arr=[];
            for pnd=1:size(run_snips,1)
                cur_trace=cur_act(run_snips(pnd,1):run_snips(pnd,2));
                cur_arr(pnd,:)=cur_trace;
            end
            run_arr(cnt,:,tnd)=mean(cur_arr,1);
            cnt=cnt+1;
        end
    end
    ini_cnt=ini_cnt+size(all_act{1},1);
end
    

cell_mat=[];
x=[1:21 21:-1:1];
figure;
for knd=1:7
    vis_err=[];
    run_err=[];
    fb_err=[];
    pb_err=[];
    cur_vis=vis_arr(:,:,knd);
    cur_run=run_arr(:,:,knd);
    cur_fb=fb_arr(:,:,knd);
    cur_pb=pb_arr(:,:,knd);
    rem=[];
    rem(:,1)=(sum(cur_vis,2)==0);
    rem(:,2)=(sum(cur_run,2)==0);
    rem(:,3)=(sum(cur_fb,2)==0);
    rem(:,4)=(sum(cur_pb,2)==0);
    rem=sum(rem,2);
    cur_vis(logical(rem),:)=[];
    cur_run(logical(rem),:)=[];
    cur_fb(logical(rem),:)=[];
    cur_pb(logical(rem),:)=[];
    
%     rem=[];
%     rem(:,1)=mean(cur_vis(:,7:10),2)-mean(cur_vis(:,1:4),2)<-0.02;
%     rem(:,2)=mean(cur_run(:,7:10),2)-mean(cur_run(:,1:4),2)<-0.02;
%     rem(:,3)=mean(cur_fb(:,7:10),2)-mean(cur_fb(:,1:4),2)<-0.02;
%     rem(:,4)=mean(cur_pb(:,7:10),2)-mean(cur_pb(:,1:4),2)<-0.02;
%     rem=sum(rem,2);
%     cur_vis(logical(rem),:)=[];
%     cur_run(logical(rem),:)=[];
%     cur_fb(logical(rem),:)=[];
%     cur_pb(logical(rem),:)=[];
    
    vis_trace=nanmean(cur_vis,1);
    run_trace=nanmean(cur_run,1);
    fb_trace=nanmean(cur_fb,1);
    pb_trace=nanmean(cur_pb,1);
    vis_trace=vis_trace+diff([min(vis_trace(1:5)) 1]);
    run_trace=run_trace+diff([min(run_trace(1:5)) 1]);
    fb_trace=fb_trace+diff([min(fb_trace(1:5)) 1]);
    pb_trace=pb_trace+diff([min(pb_trace(1:5)) 1]);
%     n=sqrt(976);
%     for und=1:21
%         err=std(cur_vis(:,und))/n;
%         err=err*1.96;
%         vis_err(:,und)=[vis_trace(und)+err vis_trace(und)-err];
%         err=std(cur_run(:,und))/n;
%         err=err*1.96;
%         run_err(:,und)=[run_trace(und)+err run_trace(und)-err];
%     end
%     for und=1:41
%         err=std(cur_fb(:,und))/n;
%         err=err*1.96;
%         fb_err(:,und)=[fb_trace(und)+err fb_trace(und)-err];
%         err=std(cur_pb(:,und))/n;
%         err=err*1.96;
%         pb_err(:,und)=[pb_trace(und)+err pb_trace(und)-err];
%     end
    subplot(1,7,knd)
    hold on
    line([5 5],[0.99 1.08],'col','k','linewidth',3)
    plot(run_trace,'k','linewidth',2)
%     y=[run_err(1,:),run_err(2,21:-1:1)];
%     fill(x,y,'k')
    plot(fb_trace,'col',[1 0.5 0],'linewidth',2)
%     y=[fb_err(1,6:26),fb_err(2,26:-1:6)];
%     fill(x,y,[1 0.5 0])
    plot(pb_trace,'y','linewidth',2)
%     y=[pb_err(1,6:26),pb_err(2,26:-1:6)];
%     fill(x,y,'y')
    plot(vis_trace,'g','linewidth',2)
%     y=[vis_err(1,:),vis_err(2,21:-1:1)];
%     fill(x,y,'g')
    xlim([0 40])
    ylim([0.99 1.09])
    cell_mat(knd,:)=[size(cur_vis,1) size(cur_run,1) size(cur_fb,1) size(cur_pb,1)];
end

% cnt=1;
% figure;
% hold on
% for gnd=1:3
%     subplot(3,3,cnt)
%     plot(run_arr(1:629,:,gnd)')
%     cnt=cnt+1;
% end
% for gnd=1:3
%     subplot(3,3,cnt)
%     plot(run_arr(630:1203,:,gnd)')
%     cnt=cnt+1;
% end
% for gnd=1:3
%     subplot(3,3,cnt)
%     plot(run_arr(1204:end,:,gnd)')
%     cnt=cnt+1;
% end



